﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Util
{
	public  class UtilStr
	{
		public static bool CheckPattern(string check, string pattern)
		{
			return Regex.IsMatch(check, pattern);
		}
		public static string RandCode(int n)
		{
			char[] arrChar = new char[]
		    {
			'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' 
		    };
			StringBuilder num = new StringBuilder();
			Random rnd = new Random(DateTime.Now.Millisecond);
			for (int i = 0; i < n; i++)
				num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
			return num.ToString();
		}
		public static string RandStr(int n)
		{
			char[] arrChar = new char[] 
		    {
			'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
			'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
			'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
			'U', 'V', 'W', 'X', 'Y', 'Z',
		    };
			StringBuilder num = new StringBuilder();
			Random rnd = new Random(DateTime.Now.Millisecond);
			for (int i = 0; i < n; i++)
				num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
			return num.ToString();
		}
		public static string CutStr(string str, int length = 100)
		{
			return str.Length <= length ? str : str.Substring(0, length) + "...";
		}
		public static string GetStrFromHtml(string Htmlstring)
		{
			Htmlstring = Htmlstring.Replace("<br/>", "\n");
			Htmlstring = Htmlstring.Replace("<br/>", "\r\n");
			Htmlstring = Htmlstring.Replace("&lt;", "<");
			Htmlstring = Htmlstring.Replace("&gt;", ">");
			Htmlstring = Htmlstring.Replace("&quot;", "\"");
			Htmlstring = Htmlstring.Replace("&nbsp;", "  ");
			Htmlstring = Htmlstring.Replace("&amp;", "");
			//删除HTML  
			Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "   ", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
			//尖括号替换
			Htmlstring.Replace("<", "");
			Htmlstring.Replace(">", "");
			Htmlstring.Replace("\r\n", "");

			return Htmlstring;
		}
	}
}